package controller

import (
	"RestaurantApp/param"
	"RestaurantApp/service"
	"RestaurantApp/utils"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"strconv"
)

type MemberController struct {
}

func (this *MemberController) Routers(ctx *gin.Engine) {
	apiGroups := ctx.Group("/api")
	apiGroups.GET("/captcha", this.captcha)
	apiGroups.POST("/vertifycha", this.vertifyCaptcha)
	apiGroups.POST("/login_pwd", this.loginByPassword)
	apiGroups.POST("/upload/avator", this.uploadAvator)
	ctx.GET("userinfo", this.getUserInfo)

}

func (this *MemberController) uploadAvator(ctx *gin.Context) {
	userId := ctx.PostForm("user_id")
	file, err := ctx.FormFile("avator")
	if err != nil {
		logger.Error(err.Error())
	}
	//检查session中用户是否已登录
	//sess := utils.GetSess(ctx, "user_"+userId)
	//if sess == nil {
	//	logger.Error("用户未登录")
	//	utils.Failed(ctx, gin.H{
	//		"message": "用户未登录",
	//	})
	//	return
	//}
	//utils.Success(ctx, gin.H{
	//	"message": "用户已登录",
	//})
	//logger.Info("用户登录鉴权成功")
	fileService := service.FileService{}
	fileService.UploadAvator(userId, file)
	utils.Success(ctx, gin.H{
		"message": "成功修改头像",
	})
}

func (this *MemberController) captcha(ctx *gin.Context) {
	utils.GenerateCaptcha(ctx)

}

func (this *MemberController) vertifyCaptcha(ctx *gin.Context) {
	var captcha utils.CaptchaResult
	err := utils.Decode(ctx.Request.Body, &captcha)
	if err != nil {
		logger.Error(err.Error())
	}
	result := utils.VertifyCaptcha(captcha.Id, captcha.VertifyValue)
	if result == true {
		logger.Info("验证成功")
		utils.Success(ctx, gin.H{
			"message": "验证码验证通过",
		})
	} else {
		logger.Info("验证失败")
		utils.Failed(ctx, gin.H{
			"message": "验证码验证失败",
		})
	}

}

func (this *MemberController) loginByPassword(ctx *gin.Context) {
	var loginParam param.LoginParam
	err := utils.Decode(ctx.Request.Body, &loginParam)
	if err != nil {
		logger.Error(err.Error())
		utils.Failed(ctx, gin.H{
			"message": "参数解析失败",
		})
	}

	flag := utils.VertifyCaptcha(loginParam.Id, loginParam.Value)
	//TODO 调试暂停验证码
	flag = true
	if flag == false {
		logger.Info("验证码错误")
		utils.Failed(ctx, gin.H{
			"message": "验证码错误",
		})
	}

	memberService := service.MemberService{}
	member := memberService.LoginByPassword(loginParam)
	if member.Id > 0 {
		logger.Info("登录成功")
		memberJson, _ := json.Marshal(member)
		userId := "user_" + strconv.Itoa(int(member.Id))
		err := utils.SetSess(ctx, userId, memberJson)
		if err != nil {
			logger.Error(err.Error())
			utils.Failed(ctx, gin.H{
				"message": "登陆失败",
			})
		}
		// 设置cookie  secure不能设置为false  否则postman调试不到
		ctx.SetCookie(utils.Cookie_Name, strconv.Itoa(int(member.Id)), utils.Cookie_Time_Length, "/", "localhost", false, false)

		utils.Success(ctx, member)
	} else {
		logger.Info("登录失败")
		utils.Success(ctx, gin.H{
			"message": "登录失败",
		})
	}

}

func (this *MemberController) getUserInfo(ctx *gin.Context) {
	cookie, err := utils.CookieAuth(ctx)
	if err != nil {
		logger.Error(err.Error())
	}
	if cookie == nil {
		utils.Failed(ctx, "cookie为空")
		return
	}
	logger.Info("cookie.value :   ", cookie.Value)
	userId := cookie.Value
	memberService := service.MemberService{}
	id, _ := strconv.Atoi(userId)
	member := memberService.GetUserInfoByUserId(id)
	if member.Id == 0 {
		logger.Info("查无此人")
		utils.Failed(ctx, "没有找到当前会话的用户")
		return
	}
	utils.Success(ctx, member)

}
